package com.burgeon.backend.SHOPBACKEND.rest.openCardTask;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.openCardTask.StoreListData;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;


public class FindStoreListView extends RESTBase {

    private final static Integer NUM = 10;

    private String ip;
    private String account;
    private String password;

    public FindStoreListView(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }


    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<StoreListData> getData(String field, String value, Integer start) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(field, value, start), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        JSONArray jsonArray = new JSONArray(str.replace("null","-")).getJSONObject(0).getJSONArray("rows");

        ArrayList<StoreListData> storeListData = new ArrayList<StoreListData>();

        for (int i = 0; i < jsonArray.length(); i++) {


            StoreListData storeData = new StoreListData();
            storeData.setId(jsonArray.getJSONArray(i).getString(0));
            storeData.setName(jsonArray.getJSONArray(i).getString(1));
            storeData.setArea(jsonArray.getJSONArray(i).getString(2));
            storeData.setBrand(jsonArray.getJSONArray(i).getString(3));
            storeData.setCount(count);

            storeListData.add(storeData);
        }
        return storeListData;
    }

    private MultiValueMap Data(String field, String value, Integer start) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("'NAME'");
        columns.add("'C_AREA_ID;NAME'");
        columns.add("'M_DIM1_ID;ATTRIBNAME'");


        params.put("columns", columns);
        params.put("table", "C_STORE");//表名

        //设置查询规则（从第几条查询，每页显示多少条数）
        params.put("range", NUM);
        params.put("count", "true");
        params.put("start", start * NUM);

        //设置排序规则
        HashMap orderby = new HashMap();
        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);
   /*     if (null != field) {
            HashMap paramss = new HashMap();
            paramss.put("column", "'" + field + "'");
            paramss.put("condition", "'" + value + "'");
            params.put("params", paramss);
        }
*/
        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();

        expr1.put("column", "'" + field + "'");
        expr1.put("condition", "'" + value + "'");

        HashMap expr2 = new HashMap();

        expr2.put("column","ISACTIVE");
        expr2.put("condition","'Y'");

        paramss.put("combine", "and");
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindStoreListView querryYCRetailout = new FindStoreListView("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(querryYCRetailout.getData("", "", 0));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
